<template>
  <div class="app-container">
    <el-tabs v-model="active" type="border-card">
      <el-tab-pane label="登录账户设置" name="first">
        <el-form ref="form" :rules="rules" :model="form">
          <el-form-item label="姓名" prop="username" label-width="80px">
            <el-input v-model="form.username" />
          </el-form-item>
          <el-form-item label="密码" prop="password" label-width="80px">
            <el-input v-model="form.password" type="password" />
          </el-form-item>
          <el-form-item label-width="80px">
            <el-button type="primary" @click="save">更新</el-button>
            <el-button @click="$router.back()">取消</el-button>
          </el-form-item>
        </el-form>
      </el-tab-pane>
      <el-tab-pane label="个人详情" name="second">
        <EmpInfo />
      </el-tab-pane>
      <el-tab-pane label="岗位信息" name="third">
        <JobInfo />
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import { userProfile } from '@/api/user'
import { saveUserInfo } from '@/api/employees'
import EmpInfo from './components/EmpInfo.vue'
import JobInfo from './components/JobInfo.vue'
export default {
  name: 'EmployeesDetail',
  components: {
    EmpInfo,
    JobInfo
  },
  data() {
    return {
      active: 'first',
      form: {},
      rules: {
        username: [
          {
            required: true, message: '请输入姓名', trigger: 'blur'
          }
        ],
        password: [
          {
            required: true, message: '请输入密码', trigger: 'blur'
          },
          {
            min: 6, max: 9, message: '密码长度6-9位', trigger: 'blur'
          }
        ]
      }
    }
  },
  created() {
    this.getProfile()
  },
  methods: {
    async getProfile() {
      const result = await userProfile(this.$route.query.id)
      this.form = result.data
    },
    save() {
      this.$refs.form.validate(async value => {
        if (value) {
          await saveUserInfo(this.form)
          this.$router.back()
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.el-form  {
  width: 450px;
  margin: 100px auto;
}
</style>
